<template v-cloak>
  <div>
    <div class="danweiguanlis">
      <div class="wrap3 flex-col">
        <div v-show="isshow">
          <div class="header">
            <div class="group1 flex-col" @click="add">
              <span class="txt2">新增单位</span>
            </div>
            <div class="group1-r">
              <span class="zhuangtai">状态</span>
              <select name id v-model="nState" @change="selecteorganization">
                <option value="-1">全部</option>
                <option value="1">启用</option>
                <option value="0">禁用</option>
              </select>
              <input class="danweimingc" type="text" v-model="sName" placeholder="单位名称" />
              <el-button type="primary" @click="select">查询</el-button>
            </div>
          </div>
          <div class="group5 flex-col">
            <el-table
              :data="selecteorganizationEmpty"
              tooltip-effect="dark"
              style="width: 100%"
              row-key
            >
              <el-table-column prop="sName" label="单位名称" show-overflow-tooltip align="center"></el-table-column>
              <el-table-column prop="sAccount" label="账号" show-overflow-tooltip align="center"></el-table-column>
              <el-table-column prop="nState" label="状态" show-overflow-tooltip align="center">
                <template scope="scope">
                  <span v-if="scope.row.nState===1">启用</span>
                  <span v-if="scope.row.nState===0">禁用</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" show-overflow-tooltip align="center">
                <template slot-scope="scope">
                  <a @click="editor(scope.row)" href="javascript:;">编辑</a>
                  <span>|</span>
                  <a href="#" @click.prevent="dialogVisible = true">删除</a>
                  <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
          <span>此操作将永久删除该文件, 是否继续?</span>
          <span slot="footer" class="dialog-footer">
            <button class="button1" @click="dialogVisible = false">取 消</button>
            <button class="button2" type="primary" @click="PostDeleteorg(scope.row)">确 定</button>
          </span>
        </el-dialog>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
        <!-- 返回 -->
        <div v-show="ishowfan">
          <div class="group4 flex-col" @click="addf">
            <span class="txt2">返回</span>
          </div>
          <div class="fanhui">
            <!--修改 表单 -->
            <div class="biaodan">
              <div>
                <label for>单位名称</label>
                <input id="id1" type="text" v-model="sName" />
              </div>
              <div>
                <label for>账号</label>
                <input id="id1" type="text" v-model="sAccount" />
              </div>
              <div>
                <label for>密码</label>
                <input id="id1" type="text" v-model="sPassword" />
              </div>
              <div>
                <label for>状态</label>
                <select class="tai" v-model="nState">
                  <option value="1">启用</option>
                  <option value="0">禁用</option>
                </select>
              </div>
              <button class="queding" @click="PostUpdateorg">确定</button>
            </div>
          </div>
        </div>
        <!-- 添加 -->
        <div v-show="istianjai">
          <div class="group4 flex-col" @click="addf">
            <span class="txt2">返回</span>
          </div>
          <div class="fanhui">
            <!-- 表单 -->
            <div class="biaodan">
              <div>
                <label for>单位名称</label>
                <input id="id1" type="text" v-model="asName" />
              </div>
              <div>
                <label for>账号</label>
                <input id="id1" type="text" v-model="asAccount" />
              </div>
              <div>
                <label for>密码</label>
                <input id="id1" type="text" v-model="asPassword" />
              </div>
              <div>
                <label for>状态</label>
                <select class="tai" v-model="anState">
                  <!-- <option>全部</option> -->
                  <option value="1">启用</option>
                  <option value="0">禁用</option>
                </select>
              </div>
              <button @click="postAddorganizations" class="queding">确定</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Coolies from 'js-cookie'
export default {
  data () {
    return {
      dialogVisible:false,
      asName: '',
      asAccount: '',
      asPassword: '',
      anState: '',
      anPid: '',
      // 搜索
      query: '',
      // 获取单位的数据
      Allorganizations: [],
      PostUpdateoEmpty: [],
      PostDeleteoEmpty: [],
      id: '',
      sName: '',
      sAccount: '',
      sPassword: '',
      // 修改暂存数据
      newlist: [{
        id: '',
        sName: '',
        sAccount: '',
        sPassword: '',
        nState: ''
      }],
      keywords: '',
      searchOps: '',
      value: '',
      input: '',
      isshow: true,
      ishowfan: false,
      istianjai: false,
      labelPosition: 'right',
      selecteorganizationEmpty: [],
      sName: '',
      nState: '-1',
    }
  },
  created () {
    this.GetAllorganizations()
    this.selecteorganization()
  },
  methods: {
    //单位查询接口
    async selecteorganization () {
      let formData = new FormData()
      formData.append('insName', this.sName)
      formData.append('innState', this.nState)
      const config = {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded"
        }
      }
      const { data: res } = await this.$http.post('/api/organization/Post_selecteorganization', formData, config)
      this.selecteorganizationEmpty = res
      console.log(res);
    },
    select () {
      this.selecteorganization()
      this.sName = ''
    },
    // 接口  获取所有单位
    async GetAllorganizations () {
      const { data: res } = await this.$http.get('/api/organization/Get_Allorganizations')
      this.Allorganizations = res
      // console.log(this.Allorganizations);
      Coolies.set('insName', this.Allorganizations[0].sName)
      Coolies.set('insAccount', this.Allorganizations[0].sAccount)
      Coolies.set('insPassword', this.Allorganizations[0].sPassword)
      Coolies.set('innState', this.Allorganizations[0].nState)
      Coolies.set('innPid', this.Allorganizations[0].nPid)
    },
    // 新增单位
    async postAddorganizations () {
      this.isshow = true
      this.istianjai = false
      this.ishowfan = false
      let formData = new FormData()
      formData.append('insName', this.asName)
      formData.append('insAccount', this.asAccount)
      formData.append('insPassword', this.asPassword)
      formData.append('innState', this.anState)
      formData.append('innPid', 1)
      const config = {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded"
        }
      }
      const { data: res } = await this.$http.post('/api/organization/Post_Addorganization', formData, config)
      // console.log(res);
      this.xznUid = res
      Coolies.set('innUid', this.xznUid)
      let allres = { insName: this.asname, insAccount: this.asAccount, insPassword: this.asPassword, innState: this.anState, nOid: 1 }
      this.Allorganizations.push(allres)
      this.Allorganizations
      // this.$router.go(0)
      this.selecteorganization()
        this.$message({
          message: '添加成功',
          type: 'success'
        });
    },
    // 修改单位
    async PostUpdateorg () {
      let formData = new FormData()
      formData.append('nOid', this.nOid)
      formData.append('sName', this.sName)
      formData.append('sAccount', this.sAccount)
      formData.append('sPassword', this.sPassword)
      formData.append('nState', this.nState)
      formData.append('nPid', 1)
      const config = {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded"
        }
      }
      const { data: res } = await this.$http.post('/api/organization/Post_Updateorganization', formData, config)
      this.PostUpdateoEmpty = res
      console.log(res);
      this.isshow = true
      this.istianjai = false
      this.ishowfan = false
      this.sName = ''
      this.nState='-1'
      this.$message({
          message: '修改成功',
          type: 'success'
        });
    },
    // 删除单位
    async PostDeleteorg (row) {
      this.dialogVisible=false
      this.nOid = row.nOid
       this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!',
          
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      let formData = new FormData()
      formData.append('innOid', this.nOid)
      const config = {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded"
        }
      }
      const { data: res } = await this.$http.post('/api/organization/Post_deleteorganization', formData, config)
      this.PostDeleteoEmpty = res
      console.log(res);
      this.selecteorganization()
      this.$message({
            type: 'success',
            message: '删除成功!'
          });
    },

    searchOp (searchOps) {
      let Lists = this.list.filter(item => {
        if (item.zhuang.includes(searchOps)) {
          return item
        }
      })
      return Lists
    },
    add () {
      this.selecteorganization()
      this.isshow = false
      this.ishowfan = false
      this.istianjai = true
    },
    addf () {
      this.isshow = true
      this.ishowfan = false
      this.istianjai = false
      this.$http.get()
      this.sName=""
      this.nState="-1"
    },

    editor (row) {
      console.log(row)
      this.nOid = row.nOid
      this.sName = row.sName;
      this.sAccount = row.sAccount;
      this.sPassword = row.sPassword;
      this.nState = row.nState;
      this.isshow = false
      this.ishowfan = true
      this.istianjai = false
    },
  }
};
</script>
<style scoped lang="css" src="./assets/index.css" />
